package com.hitechbangla.fctu.form;

import com.hitechbangla.fctu.base.aircraft.AircraftType;
import java.awt.Container;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.List;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

/**
 *
 * @author deb kanti
 */
public class ChangeAircraftType extends JFrame
{
    private List<AircraftType> listAircraftType;
    private int rowNumber;
    private JLabel[] listJLabelAircraftType;
    private JLabel[] listJLabelEdit;
    private JLabel[] listJLabelDelete;
    private int deleteIndex;
    private JButton jButtonAddAircraft;
    private Container container;
    
    public static void main(String a[])
    {
	ChangeAircraftType changeAircraftType = new ChangeAircraftType();
	changeAircraftType.setVisible(true);
    }
    
    public ChangeAircraftType()
    {
	this.setLayout(null);	
	
	this.setTitle("Aircraft Type");
	this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
	
	this.setBounds(200,200,400,800);
	this.setVisible(true);
	this.setResizable(false);	
	
	initializeComponents();
	addContentsToFrame();
	deleteIndex = -1;
	this.setAlwaysOnTop(true);
    }
    
    private void reload() 
    {
	this.repaint();
    }
    
    private void initializeComponents()
    {
	 listAircraftType = AircraftType.getAllAircraftType();
	 rowNumber = listAircraftType.size();
	 //System.out.println("After deletion, Row No: " + rowNumber);
	// initializing the first column of the table, with aircraft types
	 listJLabelAircraftType = new JLabel[listAircraftType.size()];
	 listJLabelEdit = new JLabel[listAircraftType.size()];
	 listJLabelDelete = new JLabel[listAircraftType.size()];
	
	 // initializing the aircraft type column
	 for(int i=0;i<rowNumber;i++)
	 {
	     listJLabelAircraftType[i] = new JLabel();
	     listJLabelAircraftType[i].setText(listAircraftType.get(i).getTypename());	    
	 }
	 
	 // initializing the edit column
	 for(int i=0;i<rowNumber;i++)
	 {
	     ImageIcon imageIcon = new ImageIcon("images/imagesedit.jpg");
	     listJLabelEdit[i] = new JLabel();
	     listJLabelEdit[i].setIcon(imageIcon);
	     listJLabelEdit[i].setToolTipText("Edit");
             listJLabelEdit[i].setCursor(new Cursor(Cursor.HAND_CURSOR));
	     
	     listJLabelEdit[i].addMouseListener(new EditButtonListener());	     
	 }
	 
	 // initializing the delete column
	 for(int i=0;i<rowNumber;i++)
	 {
	     ImageIcon imageIcon = new ImageIcon("images/imagesdelete.jpg");
	     listJLabelDelete[i] = new JLabel();
	     listJLabelDelete[i].setIcon(imageIcon);
	     listJLabelDelete[i].setToolTipText("Delete");
             listJLabelDelete[i].setCursor(new Cursor(Cursor.HAND_CURSOR));
	     
	     listJLabelDelete[i].addMouseListener(new DeleteButtonListener());
	     //listJLabelEdit[i].setText(listAircraftType.get(i).getTypename());
	 }	 
	 
	jButtonAddAircraft = new JButton("Add New Aircraft");
	jButtonAddAircraft.addMouseListener(new AddButtonListener());	 
	 
    }
    
    private void addContentsToFrame()
    {	
	container = this.getContentPane();	
	container.removeAll();
	
	for(int i=0;i<rowNumber;i++)
	{
	    container.add(listJLabelAircraftType[i]);
	    container.add(listJLabelEdit[i]);
	    container.add(listJLabelDelete[i]);
	}
	
	container.add(jButtonAddAircraft);	
	
	int y = 65;
	int x = 50;
	
	
	for(int i=0;i<rowNumber;i++)
	{  	    
	    Dimension dimension = listJLabelAircraftType[i].getPreferredSize(); 	    
	    listJLabelAircraftType[i].setBounds(x, y , dimension.width, dimension.height);	    
	    y+=40;
	}
	
	y = 65;
	x = 130;
	
	for(int i=0;i<rowNumber;i++)
	{  	    
	    Dimension dimension = listJLabelEdit[i].getPreferredSize(); 	    
	    listJLabelEdit[i].setBounds(x, y , dimension.width, dimension.height);	    
	    y+=40;
	}	
	
	y = 65;
	x = 210;
	
	for(int i=0;i<rowNumber;i++)
	{  	    
	    Dimension dimension = listJLabelDelete[i].getPreferredSize(); 	    
	    listJLabelDelete[i].setBounds(x, y , dimension.width, dimension.height);	    
	    y+=40;
	}
	this.setBounds(0, 0, 300, y + 100);
	x = 50;	
	y = 25;
	
	
	
	Dimension dimension = jButtonAddAircraft.getPreferredSize();
	jButtonAddAircraft.setBounds(x, y, dimension.width, dimension.height);
	
	////System.out.println("from changeaircrafttype: button condition x - " + x + ", y - " + y);
	
	//this.revalidate();
	//this.repaint();
	reload();
    }
    
    class AddButtonListener implements MouseListener
    {
	@Override
	public void mouseClicked(MouseEvent e) 
	{
	    AddAircraftType addAircraftType = new AddAircraftType(listAircraftType);
	    addAircraftType.setVisible(true);
	    AircraftType.updateAircraft(listAircraftType);
	    initializeComponents();
	    addContentsToFrame();
	}

	@Override
	public void mousePressed(MouseEvent e) 
	{
	    
	}

	@Override
	public void mouseReleased(MouseEvent e) 
	{
	    
	}

	@Override
	public void mouseEntered(MouseEvent e) 
	{
	    
	}

	@Override
	public void mouseExited(MouseEvent e) 
	{
	    
	}
	
    }
    
    class EditButtonListener implements MouseListener
    {
	@Override
	public void mouseClicked(MouseEvent e) 
	{
	    JLabel labelClicked = (JLabel)e.getComponent();
	    for(int i=0;i<rowNumber;i++)
	    {
		if(listJLabelEdit[i] == labelClicked)
		{
		    EditAircraftType editAircraftType = new EditAircraftType(listAircraftType,i);
		    editAircraftType.setVisible(true);		    
		    
		    ////System.out.println("from changeaircrafttype: after editing, size: " + listAircraftType.size());
		    
		    
		    for(int j=0;i<rowNumber;i++)
		    {
			////System.out.println(listAircraftType.get(i).getTypename());
		    }
		    
		    initializeComponents();
		    addContentsToFrame();
		    break;
		}
	    }
	}

	@Override
	public void mousePressed(MouseEvent e) 
	{
	    
	}

	@Override
	public void mouseReleased(MouseEvent e) 
	{
	    
	}

	@Override
	public void mouseEntered(MouseEvent e) 
	{
	    
	}

	@Override
	public void mouseExited(MouseEvent e) {
	    
	}	
    }
    
    
    class DeleteButtonListener implements MouseListener
    {
	@Override
	public void mouseClicked(MouseEvent e) 
	{
	    JLabel labelClicked = (JLabel)e.getComponent();
	    for(int i=0;i<rowNumber;i++)
	    {
		if(listJLabelDelete[i] == labelClicked)
		{
		    String title = "Delete Aircraft Type";
		    String message = "Are you sure?";
		    
		    int answer = JOptionPane.showConfirmDialog(null, message, title, JOptionPane.YES_NO_OPTION);
		    
		    if(answer == JOptionPane.YES_OPTION)
		    {
			////System.out.println("from changeaircrafttype: yes button clicked");
			deleteIndex = i;
			
			listAircraftType.remove(deleteIndex);
	    
			for(AircraftType aircraftType: listAircraftType)
			{
			    ////System.out.println("from changeaircrafttype:" + aircraftType.getTypename());
			}
			AircraftType.updateAircraft(listAircraftType);
			//reload();
			initializeComponents();
			addContentsToFrame();
			break;
		    }		    
		}
	    }
	    
	    
	    
	    
	    ////System.out.println("from changeaircrafttype: label  " + deleteIndex + " deleted" );
	}

	@Override
	public void mousePressed(MouseEvent e) 
	{
	    
	}

	@Override
	public void mouseReleased(MouseEvent e) 
	{
	    
	}

	@Override
	public void mouseEntered(MouseEvent e) 
	{
	    
	}

	@Override
	public void mouseExited(MouseEvent e) 
	{
	    
	}
	
    }
    
}
